<!-- 签约专家 -->
<template>
  <publicPage>
    <view class="addFriendPage">
      <view class="bgf borderB px-15 py-7">
        <u-search
          v-model="getParams.userName"
          :disabled="false"
          placeholder="搜索"
          clearabled
          @search="_searchFn"
          @custom="_searchFn"
        ></u-search>
      </view>
      <view class="bgf" v-if="status === 1">
        <u-cell :border="false" isLink @click="scanCode">
          <view slot="title" class="disFlxAC">
            <view class="qrImg">
              <image
                src="@/static/image/image220701(5).png"
                style="width: 48rpx; height: 48rpx"
              />
            </view>
            <view>
              <view class="fz-16">扫一扫</view>
              <view class="color8 pt-4">扫描专家二维码</view>
            </view>
          </view>
        </u-cell>
      </view>

      <scroll-view
        v-if="status === 2 && list.length"
        scroll-y
        style="height: calc(100% - 50px)"
        @scrolltolower="scrolltolower"
      >
        <view
          v-for="(item, index) in list"
          :key="index"
          class="forBox bgf mt-15 mx-15"
          @click="goPage(`/pagesC/addFriend/addConsult?exp_id=${item.id}`)"
        >
          <InfoCard :item="item" />
        </view>

        <view class="safe-height"></view>
      </scroll-view>
      <view
        class="defaultPage disFlxCC"
        style="height: calc(100% - 50px)"
        v-if="status === 2 && !list.length"
      >
        <view class="fz-18 color8">暂无结果</view>
      </view>
    </view>
  </publicPage>
</template>

<script>
import InfoCard from "@/components/infoCard.vue";
import orderApi from "@/api/order.js";

export default {
  components: {
    InfoCard,
  },
  data() {
    return {
      status: 1, // 搜索前: 1, 搜索后 2
      list: [],
      getParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        userName: "",
      },
    };
  },
  watch: {
    'getParams.userName'(nval){
      if(!nval){
        this.status = 1
      }
    }
  },
  methods: {
    // 搜索专家列表
    async getList() {
      const { data, total } = await orderApi.searchexpList(this.getParams);
      this.list = this.getParams.pageNum === 1 ? data : [...this.list, ...data];
      this.getParams.total = total;
      this.status = 2
    },
    // 搜索专家
    _searchFn() {
      const that = this;
      if(!that.getParams.userName) return
      // u-view内部节流函数
      uni.$u.throttle(() => {
        console.log("搜索专家:", that.getParams.userName);
        that.getParams.pageNum === 1;
        that.getParams.total === 0;
        that.getList();
      }, 1000);
    },
    scrolltolower() {
      if (this.list.length !== this.getParams.total) {
        // u-view内部节流函数
        uni.$u.throttle(() => {
          this.getParams.pageNum++;
          this.getList();
          console.log(this.getParams.pageNum, "滚动加载");
        }, 2000);
      } else {
        console.log("没有更多了");
      }
    },
      // 点击扫一扫
      scanCode() {
        uni.scanCode({
          success: (res) => {
            console.log(res, "扫码参数");
            
            if (res.path) {
              this.goPage(`/${res.path}`);
            }else if(res.scanType === 'QR_CODE' && res.result.indexOf('mp_inspection') !== -1){
                this.goPage(`/pagesC/reserveExp/inspection?q=${encodeURIComponent(res.result)}`)
            } else {
              uni.showToast({
                title: "暂不支持的二维码",
                icon: "none",
              });
            }
          },
        });
      },
  },
};
</script>
<style lang='scss' scoped>
.addFriendPage {
  height: 100%;
  .qrImg {
    background-color: #2dbcbe;
    border-radius: 10rpx;
    width: 80rpx;
    height: 80rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 40rpx;
  }
  .forBox {
    border-radius: 20rpx;
    overflow: hidden;
  }
}
</style>